//
//  BaseController.m
//  collection
//
//  Created by 张靖 on 2/27/12.
//  Copyright (c) 2012 Tsinghua University. All rights reserved.
//

#import "BaseController.h"
#import "HomepageRootController.h"
#import "MessageRootController.h"
#import "UserProfileController.h"
#import "MyInfoController.h"
#import "ManageRootController.h"
#import "PublishTextController.h"
#import "CollectionDataManager.h"
#import "AppDelegate.h"

@interface BaseController()

@property (nonatomic, strong)   NSArray*    tabControllers;
@property (nonatomic) int curIndex;
@property (nonatomic, strong)   IBOutlet    UIView*     surfaceView;

- (IBAction)onClickTab:(id)sender;
- (void)selectTab:(int)index;
- (void)buildSubControllers;

@end

@implementation BaseController

@synthesize tabControllers = _tabControllers;
@synthesize curIndex = _curIndex;
@synthesize surfaceView     = _surfaceView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc {
    
    self.tabControllers = nil;
    self.surfaceView    = nil;
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)loadView {
    
    [super loadView];
    
    [self buildSubControllers];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.surfaceView    = nil;
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    UIViewController* viewController = [self.tabControllers objectAtIndex:self.curIndex];
    [viewController viewWillAppear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Implementation of UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    BOOL isPic;
    if (buttonIndex == 0) {
        isPic = YES;
    }
    else if (buttonIndex == 1) {
        isPic = NO;
    }
    else if (buttonIndex == actionSheet.cancelButtonIndex) {
        return;
    }
    
    PublishTextController* publishController = [[PublishTextController alloc] initWithNibName:@"PublishTextController" bundle:nil];
    publishController.isPicture = isPic;

    [self.navigationController presentModalViewController:publishController animated:YES];
    [publishController release];
}

#pragma mark - Private methods

- (IBAction)onClickTab:(id)sender {
    
    UIButton* tagButton = (UIButton*)sender;
    int index = tagButton.tag - 21;
    if (index < 0 || index > 4) {
        return;
    }
    
    [self selectTab:index];
}

- (void)selectTab:(int)index {
    
    if (index < 0 || index > 4) {
        return;
    }
    
    // Publish
    if (index == 2) {
        UIActionSheet* publishActionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                                        delegate:self 
                                                               cancelButtonTitle:@"取消" 
                                                          destructiveButtonTitle:nil 
                                                               otherButtonTitles:@"图片", @"文字", nil];
        [publishActionSheet showInView:self.view];
        [publishActionSheet release];
        
        return;
    }
    
    // Change the tab
    for (int tag = 21; tag <= 25; tag++) {
        UIButton* tagButton = (UIButton*)[self.surfaceView viewWithTag:tag];
        tagButton.selected = (tag == (index + 21));
    }
    
    // Change the content
    for (int i = 0; i < [self.tabControllers count]; i++) {
        UIViewController* viewController = [self.tabControllers objectAtIndex:i];
        viewController.view.hidden = (i != index);
    }
    
    self.curIndex = index;
}

- (void)buildSubControllers {
    
    CGRect frame = self.view.frame;
    frame.size.height -= 44;
    
    // Homepage
    UIViewController* homepageController = [[HomepageRootController alloc] initWithNibName:@"HomepageRootController" bundle:nil];
    //    homepageController.view.frame = frame;
    
    // Message
    UIViewController* messageController = [[MessageRootController alloc] initWithNibName:@"MessageRootController" bundle:nil];
    
    
    // Publish
    UIViewController* publishFakeController = [[UIViewController alloc] init];
    
    // User Profile
    UserProfileController* myAccountController = [[UserProfileController alloc] initWithNibName:@"UserProfileController" bundle:nil];
    CollectionDataManager* sharedDataManager = [CollectionDataManager sharedInstance];
    myAccountController.userID = sharedDataManager.currentLoginUser.userID;
    //    UIViewController* myInfoController = [[MyInfoController alloc] initWithNibName:@"MyInfoController" bundle:nil];
    
    // Management
    UIViewController* manageController = [[ManageRootController alloc] initWithNibName:@"ManageRootController" bundle:nil];
        
    self.tabControllers = [NSArray arrayWithObjects:homepageController, messageController, publishFakeController, myAccountController, manageController, nil];
    
    [self.view addSubview:homepageController.view];
    [self.view addSubview:messageController.view];
    [self.view addSubview:publishFakeController.view];
    [self.view addSubview:myAccountController.view];
    [self.view addSubview:manageController.view];
    [self.view bringSubviewToFront:self.surfaceView];

    [homepageController release];
    [messageController release];
    [publishFakeController release];
    [myAccountController release];
    [manageController release];

    [self selectTab:0];
}


@end